package com.hualan.sfzy.controller.course;

import com.hualan.sfzy.entity.course.Course;
import com.hualan.sfzy.service.course.CourseService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @PostMapping("/course")
    public Result add(@RequestBody Course course) {
        return courseService.add(course)>0?Result.success():Result.error();
    }
    @DeleteMapping("/course/{id}")
    public Result remove(@PathVariable int id) {
        return courseService.remove(id)>0?Result.success():Result.error();
    }
    @DeleteMapping("/course")
    public Result removeBatch(Integer[] ids) {
        return courseService.removeBatch(ids)>0?Result.success():Result.error();
    }
    @PutMapping("/course")
    public Result edit(@RequestBody Course course) {
        return courseService.edit(course)>0?Result.success():Result.error();
    }
    @GetMapping("/course")
    public Result find() {
        List<Course> courses = courseService.find();
        return Result.success(courses);
    }
    @GetMapping("/course/{id}")
    public Result findById(@PathVariable int id) {
        Course course = courseService.findById(id);
        return Result.success(course);
    }
    @GetMapping("/course/page")
    public Object findByPage(
            @RequestParam(value = "pageNum",defaultValue = "1",required = false)
            Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5",required = false)
            Integer pageSize,
            Course course
    ) {
        Map<String,Object> map = courseService.findByPage(pageNum,pageSize,course);
        return Result.success(map);
    }
    @PutMapping("/course/{id}/updateStudyNumber")
    public Result updateStudyNumber(@PathVariable Integer id) {
        int count = courseService.updateStudyNumber(id);
        return count > 0 ? Result.success() : Result.error();
    }

    @GetMapping("/coursesort")
    public Result find(@RequestParam(value = "sortType", required = false) String sortType,
                       @RequestParam(value = "priceRange", required = false) String priceRange) {
        List<Course> courses = courseService.find(sortType, priceRange);
        return Result.success(courses);
    }


}

